<?php
namespace Home\Controller;
use Think\Controller;
use App\Controller\BaseController;
class MidiclubController extends BaseController {

    public function _initialize(){
        //php判断客户端是否为手机
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS"))
        header("Location:http://xuancode.com/midi/index.php/Web/");
        //如果session存在，传递用户信息
        $midiuser = session('midiuser');
        $this->assign('midiuser',$midiuser);
    }

    public function index(){
    	//筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['midiclub_name'])) {
            $data['midiclub_name'] = array('like',"%".trim($_GET['midiclub_name'])."%");
        }
        unset($data['p']);
        $data['midiclub_display'] = 1;

        $count = M('midiclub')->alias('m')->join('left join midi_city as c on m.midiclub_city = c.city_id')->where($data)->count();
        $page = getpage($count,10);
        $midiclub_arr = M('midiclub')->alias('m')->join('left join midi_city as c on m.midiclub_city = c.city_id')->where($data)->order('midiclub_id desc')->limit($page->firstRow, $page->listRows)->select();

        $sql = "select * from midi_city order by CONVERT( city_name USING gbk ) COLLATE gbk_chinese_ci ASC";
        $city_arr = M()->query($sql);
        $this->assign('city_arr',$city_arr);
        // $arr = [];
        // foreach ($city_arr as $k => $v) {
        //     $arr[] = [$v['city_id']=>$v['city_name']];
        // }
        // $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
        // $res = file_put_contents('city',$json);
        // var_dump($res);
        // exit();
        $this->assign('midiclub_arr', $midiclub_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function club_details(){
    	$club_arr = M('midiclub')->field('midiclub_id,midiclub_name,midiclub_address,midiclub_lng,midiclub_lat,midiclub_pic,midiclub_content')->where(array('midiclub_id'=>$_GET['id']))->find();
    	$this->assign('club_arr',$club_arr);
    	$this->display();
    }
}